//
//  DXWHomePageTitleBarController.m
//  pandaTV
//
//  Created by 淡馨慰 on 16/10/17.
//  Copyright © 2016年 淡馨慰. All rights reserved.
//

#import "DXWHomePageTitleBarController.h"
#import "DXWAddChannelController.h"

@interface DXWHomePageTitleBarController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>

/** collectionView */
@property (nonatomic, weak) UICollectionView *collectionView;
/** titleView */
@property (nonatomic, weak) UIScrollView *titleView;
/** 下划线 */
@property (nonatomic, weak) UIView *underLineView;
/** 存储按钮 */
@property (nonatomic, strong) NSMutableArray *buttons;
/** 当前选择的按钮 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 初始化属性 */
@property (nonatomic, assign) BOOL isInitialized;

@end

@implementation DXWHomePageTitleBarController

static NSString * const ID = @"colectionViewCell";
- (NSMutableArray *)buttons
{
    if (_buttons == nil) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //2.添加UICollectionView
    [self setupCollectionView];
    //1.添加titleView
    [self setupTopTitleView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
}
//模拟tabBar
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //懒加载titleView上的按钮
    if (_isInitialized == NO) {
        
        [self setuptitleBar];
        _isInitialized = YES;
    }
}
//添加顶部titleView
- (void)setupTopTitleView
{
    //增强复用性：可滚动
    UIScrollView *titleView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, screenW,40)];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yule_first_line"]];
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setImage:[UIImage imageNamed:@"addbutton"] forState:UIControlStateNormal];
    addBtn.frame = CGRectMake(screenW - 40, 0, 40, 40);
    [addBtn addTarget:self action:@selector(addChannelClick) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:addBtn];
    imageView.frame = titleView.bounds;
    [titleView insertSubview:imageView atIndex:0];
    titleView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleView];
    _titleView = titleView;
    _titleView.delegate = self;
}
//添加频道子控件按键
- (void)addChannelClick
{
    DXWAddChannelController *addVC = [[DXWAddChannelController alloc]init];
    addVC.hidesBottomBarWhenPushed = YES;
    addVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController pushViewController:addVC animated:YES];
}

#pragma mark - UIScrollViewDelegate
// 减速完成的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 获取页码
    NSInteger page = scrollView.contentOffset.x / screenW;
    
    // 1.选中按钮
    [self selButton:self.buttons[page]];
}

//添加button到titleView
- (void)setuptitleBar
{
    //根据子控件个数,循环添加
    NSInteger count = self.childViewControllers.count;
    CGFloat btnW = screenW / count ;
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnH = _titleView.dxw_height;
    
    //根据子控件个数,添加btn
    for (int i = 0; i < count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btnX = i * btnW;
        UIViewController *vc = self.childViewControllers[i];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:dxw_MainColor forState:UIControlStateSelected];
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [_titleView addSubview:btn];
        
        [btn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.buttons addObject:btn];
        
        if (i == 0) {
            // 添加下划线
            // 下划线位置 = 0个按钮有关系
            // h = 2
            // w = btn.titleLable.w
            // underLine.centerX = btn.centetX
            // y = btnH - h
            UIView *underLineV = [[UIView alloc] init];
            underLineV.backgroundColor = dxw_MainColor;
            [self.titleView addSubview:underLineV];
            underLineV.dxw_height = 2;
            // 按钮的文字的尺寸,并不会马上计算,不是一设置标题,就马上计算尺寸
            [btn.titleLabel sizeToFit];
            underLineV.dxw_width = btn.dxw_width - 10;
            underLineV.dxw_y = btn.dxw_height - 2;
            underLineV.dxw_centerX = btn.dxw_centerX;
            _underLineView = underLineV;
            //选择第0个按钮
            [self titleClick:btn];
        }
    }
}
#pragma mark - 点击标题就会调用
- (void)titleClick:(UIButton *)button
{
    NSInteger i = button.tag;
    // 1.选中按钮
    [self selButton:button];
    
    // 2.滚动到对应位置
    CGFloat offsetX = i * screenW;
    
    _collectionView.contentOffset = CGPointMake(offsetX, 0);
    
}

#pragma mark - 选中按钮
- (void)selButton:(UIButton *)button
{
    _selectedButton.selected = NO;
    button.selected = YES;
    _selectedButton = button;
    
    // 修改下划线位置
    [UIView animateWithDuration:0.25 animations:^{
        
        _underLineView.dxw_centerX = button.dxw_centerX;
    }];
}
//添加collectionView
- (void)setupCollectionView
{
    //1.创建布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(screenW, screenH);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//水平滚动
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    //2.创建collectionView
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:collectionView];
    _collectionView = collectionView;
    
    //3.设置属性
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;//开启分页
    
    //4.注册cell
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
    
    
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.childViewControllers.count;
    //    return self.childViewControllers.count;
}

// 当cell要显示的时候,马上把对应子控制器的tableView添加
// 作用:返回cell样式
// 什么时候调用,当cell要显示的时候就会调用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    //移除之前cell的view,添加新的cell -- 由于缓存池
    UIViewController *vc = self.childViewControllers[indexPath.row];
    //取出对应cell的控制器
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //显示titleView
    vc.view.frame = CGRectMake(0, 0, screenW, screenH);
    [cell.contentView addSubview:vc.view];
    return cell;
}





@end
